<?php
class PicController extends Zend_Controller_Action {
	/**
	 * The default action - show the home page
	 */
	private $dst_img, $ox, $oy, $src_img;
	public function preDispatch() {
		$front = Zend_Controller_Front::getInstance ();
		$front->setParam ( 'noViewRenderer', true );
		header ( "Content-Type: $b->pic_type" );
		header ( "Content-Disposition: inline; filename=$this->id.jpg" );
		
		$id = $this->_getParam ( 'id' );
		$a = new Pic ( );
		$b = $a->find ( $id )->current ();
		if ($b->pic_bits) {
			$this->src_img = imagecreatefromstring ( $b->pic_bits );
		} else {
			$filename = './html/upfile/pic/' . $b->src;
			
			$this->src_img = imagecreatefrompng ( $filename );
			
		//			$handle = fopen ( $filename, "r" );
		//			$contents = fread ( $handle, filesize ( $filename ) );
		//			fclose ( $handle );
		//			$b->pic_bits = $contents;
		//			$b->save ();
		}
		
		$this->ox = imagesx ( $this->src_img );
		$this->oy = imagesy ( $this->src_img );
	}
	public function viewAction() {
		$size = $this->_getParam ( 's', 400 );
		if ($size < $this->ox) {
			$nx = $size;
			$ny = $nx * $this->oy / $this->ox;
		} else {
			$nx = $this->ox;
			$ny = $this->oy;
		}
		if ($this->_getParam ( 'h' )) {
			$this->dst_img = imagecreatetruecolor ( $nx, $this->_getParam ( 'h' ) );
			//            if( $this->_getParam('h')>$ny ){
		//
		//            }
		} else {
			$this->dst_img = imagecreatetruecolor ( $nx, $ny );
		}
		imagecopyresized ( $this->dst_img, $this->src_img, 0, 0, 0, 0, $nx, $ny, imagesx ( $this->src_img ), imagesy ( $this->src_img ) );
		
		if ($nx > 199 && $ny > 35) {
			$black = imagecolorallocatealpha ( $this->dst_img, 255, 255, 255, 45 );
			imagettftext ( $this->dst_img, 15, 0, 10, 30, $black, '../georgiaz.ttf', "www.zy2f.com" );
		}
	}
	public function postDispatch() {
		imagejpeg ( $this->dst_img, null, 100 );
		//		imagegif ( $this->dst_img );
		//        		imagepng ( $this->dst_img );
		imagedestroy ( $this->src_img );
		imagedestroy ( $this->dst_img );
	}
}
